<html>
    <body>
        <script>

            //189000503  左香菊
            //1
            const promise = new Promise(function(executor){
                function Promise(executor){
                    executor(resolve, reject);
                    function resolve(){}
                    function reject(){}
                }


                //2
                function Promise(executor){
                    let _this = this;
                    _this.$$status = 'pending';
                    executor(resolve.bind(this), reject.bind(this));
                    function resolve(){
                        if(_this.$$status === 'pending'){
                            _this.$$status = 'full';
                        }
                    }
                    function reject(){
                        if(_this.$$status === 'pending'){
                            _this.$$status = 'fail';
                        }
                    }
                }
            })


            //3
            Promise.prototype.then = function(full, fail){
                this.successCallback = full;
                this.failCallback = fail;
            };

            //4
            function Promise(executor){
                let _this = this;
                _this.$$status = 'pending';
                _this.failCallBack = undefined;
                _this.successCallBack = undefined;
                _this.error = undefined;
                executor(resolve.bind(_this), reject.bind(_this));

                function resolve(params){
                    if(_this.$$status ==='pending'){
                        _this.$$status = 'success'
                        _this.successCallBack(params)
                    }
                }

                function reject(params){
                    if(_this.$$status === 'pending'){
                        _this.$$status = 'fail'
                        _this.failCallBack(params)
                    }
                }
            

            //5
            new Promise(function(res, rej){
                setTimeout(() => res('成功'), 0);
            }).then(result => console.log(result));
        }

        </script>
    </body>
</html>